<template>
  <el-button type="success" size="middle" @click="show = !show">
    {{ show ? '隐藏': '显示' }}
  </el-button>
  <kt-vo-page
    v-if="show"
    name="招聘渠道"
    modelWidth="450px"
    :crupDatas="crupDatas"
    :searchProps="searchProps"
    :tableOptions="tableOptions"
    :treeDatas="treeDatas"
    treeNodeMapKey="channelStatus"
    :selectAPI="selectAll"
    :crupAPI="saveChannel"
    :delAPI="delChannel"
    exportUrl="recruitmentChannelExport">
  </kt-vo-page>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { VoInput, VoSelect } from '../../packages/index'

const show = ref(true)

const crupDatas = ref([
  [
    new VoInput({
      label: '渠道编码：',
      value_key: 'channelCode',
      type: 'input',
      required: true,
      disabled: true,
    }),
    new VoInput({
      label: '渠道名称：',
      value_key: 'channelName',
      required: true,
    }),
    new VoSelect({
      label: '渠道类型：',
      value_key: 'channelType',
      required: true,
      data: [{ label: 'A', value: 'A' }, { label: 'B', value: 'B' }],
    }),
    new VoSelect({
      label: '渠道状态：',
      value_key: 'channelStatus',
      required: true,
      data: [{ label: '启用', value: '1' }, { label: '禁用', value: '0' }],
    }),
    new VoInput({
      label: '渠道简介：',
      value_key: 'dis',
      type: 'textarea',
      rows: 3,
      placeholder: '请输入渠道简介',
    }),
  ],
])

const searchProps = ([
  new VoInput({
    label: '渠道名称',
    value_key: 'channelName',
  }),
  new VoSelect({
    label: '渠道类型',
    value_key: 'channelType',
    data: [{ label: 'A', value: 'A' }, { label: 'B', value: 'B' }],
  }),
  new VoSelect({
    label: '渠道状态',
    value_key: 'channelStatus',
    data: [{ label: '启用', value: '1' }, { label: '禁用', value: '0' }],
  }),
])

const tableOptions = ref([
  { label: '渠道编号', prop: 'channelCode' },
  { label: '渠道名称', prop: 'channelName' },
  { label: '渠道类型', prop: 'channelType', data: [{ label: 'A', value: 'A' }, { label: 'B', value: 'B' }] },
  { label: '渠道状态', prop: 'channelStatus', data: [{ label: '启用', value: '1' }, { label: '禁用', value: '0' }], },
  { label: '创建人', prop: 'createMan'},
  { label: '创建时间', prop: 'createTime' },
  { label: '更新人', prop: 'updateMan'},
  { label: '更新时间', prop: 'updateTime' },
  { label: '操作', slot: 'actions' },
])

const treeDatas = [
  {
    id: 0,
    label: '渠道状态A',
    children: [
      {
        id: 1,
        label: '渠道状态B',
      },
    ],
  },
]

async function saveChannel() {
  console.log('saveChannel')
  return {
    flag: true,
    data: '操作成功',
  }
}

async function delChannel() {
  console.log(delChannel)
  return {
    flag: true,
    data: '操作成功',
  }
}

async function selectAll(queryData) {
  const records = [
    { channelCode: 1, channelName: 'test1', channelType: 'A', channelStatus: '1', createMan: 'admin', createTime: '2023/1/29 14:29:55', updateMan: 'admin', updateTime: '2023/1/29 14:29:55', },
    { channelCode: 1, channelName: 'test2', channelType: 'B', channelStatus: '1', createMan: 'admin', createTime: '2023/1/29 14:29:55', updateMan: 'admin', updateTime: '2023/1/29 14:29:55', },
    { channelCode: 1, channelName: 'test3', channelType: 'B', channelStatus: '1', createMan: 'admin', createTime: '2023/1/29 14:29:55', updateMan: 'admin', updateTime: '2023/1/29 14:29:55', },
    { channelCode: 1, channelName: 'test4', channelType: 'A', channelStatus: '1', createMan: 'admin', createTime: '2023/1/29 14:29:55', updateMan: 'admin', updateTime: '2023/1/29 14:29:55', },
    { channelCode: 1, channelName: 'test5', channelType: 'A', channelStatus: '0', createMan: 'admin', createTime: '2023/1/29 14:29:55', updateMan: 'admin', updateTime: '2023/1/29 14:29:55', },
  ]
  return {
    data: {
      records: records.filter(record => {
        for (let key in queryData) {
          if (record[key] && (queryData[key])) {
            if (!(record[key].toString().includes(queryData[key]))) {
              return false
            }
          }
        }
        return true
      }),
      total: 2,
    }
  }
}
</script>